import pymysql

def batchUpdate():
    conn = pymysql.connect(
        host="127.0.0.1",  # 替换为实际的主机名
        user="root",  # 替换为实际的用户名
        password="520777",  # 替换为实际的密码
        database="house"  # 替换为实际的数据库名
    )
    cursor = conn.cursor()

    filter = ["卖掉了", "不卖了", "空号", "下架", "停机", "只租"]
    for f in filter:
        sql = f"select distinct house_id from house_follow where house_id  in (select house_id from house h where create_time >= '2024-03-01 00:00:00' and is_del = 0) and content like '%{f}%';"
        cursor.execute(sql)

        data = cursor.fetchall()
        if len(data) == 0:
            print(f"查数据为空")
            continue
        
        # 将数据每100条一批插入数据库
        batch_size = 100
        for i in range(0, len(data), batch_size):
            batch_data = data[i:i+batch_size]
            # 分批执行
            for h in batch_data:
                try:
                    upSql = f"update house set is_del = 1 where house_id = '{h[0]}';"
                    cursor.execute(upSql)
                    print(f"将{h[0]}的is_del改为1")
                except Exception as e:
                    print(e)
        
            conn.commit()

    cursor.close()
    conn.close()
    
# 主函数
def main():
    # 批量更新数据
    batchUpdate()


if __name__ == '__main__':
    main()
